/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

/**
 *
 * @author semente-note-1
 */
import org.hibernate.FlushMode;
import org.hibernate.Session;
 
public abstract class DAO {
 
    private static final ThreadLocal THREAD = new ThreadLocal();
 
    public static Session getSession() {
        Session session = (Session) DAO.THREAD.get();
        if (session == null) {
            session = NewHibernateUtil.getSessionFactory().openSession();
            DAO.THREAD.set(session);
            getSession().setFlushMode(FlushMode.COMMIT);
        }
        return session;
    }
 
    protected static void begin() {
        getSession().beginTransaction();
    }
 
    protected static void commit() {
        getSession().getTransaction().commit();
    }
 
    protected static void rollback() {
        getSession().getTransaction().rollback();
        getSession().close();
        DAO.THREAD.set(null);
    }
 
    protected static void flush() {
        getSession().flush();
    }
 
    protected static void close() {
        getSession().close();
        DAO.THREAD.set(null);
    }
}
